const DEEPSEEK_URL = 'https://api.deepseek.com/chat/completions'
const API_KEY = 'sk-6ad59c353c6f4b82bdb8cdc7dcd98fe4' // 替换为你的 key，注意安全性，不建议直接写死在前端

export interface Message {
    role: 'user' | 'system' | 'assistant'
    content: string
}

// 定义响应类型
interface DeepSeekResponse {
    choices: {
        message: {
            content: string
        }
    }[]
}

export const callDeepSeek = (messages: Message[]): Promise<string> => {
    return new Promise((resolve, reject) => {
        uni.request({
            url: DEEPSEEK_URL,
            method: 'POST',
            header: {
                'Content-Type': 'application/json',
                'Authorization': `Bearer ${API_KEY}`
            },
            data: {
                model: 'deepseek-chat',
                messages,
                temperature: 0.9
            },
            success: (res) => {
                // 👇 添加类型断言
                const data = res.data as DeepSeekResponse

                if (res.statusCode === 200 && data.choices?.[0]?.message?.content) {
                    resolve(data.choices[0].message.content)
                } else {
                    reject('无有效响应')
                }
            },
            fail: (err) => {
                reject(err)
            }
        })
    })
}